//
//  StoryDetailViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/12.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "StoryDetailViewController.h"
#import "RmdStoryDetailBannerItem.h"
#import "RmdStoryDetailListItem.h"
#import "RmdStoryDetailHeaderView.h"
#import "RmdStoryDetailCell.h"
#import <UMSocial.h>
@interface StoryDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>

@property(nonatomic,strong)UITableView *tableView;
// banner array
@property (nonatomic, strong) NSMutableArray *bannerArray;
// cell array
@property (nonatomic, strong) NSMutableArray *listArray;
@property(nonatomic,strong)RmdStoryDetailBannerItem *rmdStoryDetailBannerItem;

@property(nonatomic,strong)RmdStoryDetailHeaderView *rmdStoryDetailHeaderView;
@property(nonatomic,strong)UIImageView *backImageView;

@end

@implementation StoryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"故事详情";
    
    [self addTableView];
    
    

}


-(void)addTableView{
    self.bannerArray = [NSMutableArray array];
    self.listArray = [NSMutableArray array];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.tableView];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    
    UIButton *right=[UIButton buttonWithType:(UIButtonTypeSystem)];
    
    right.frame = CGRectMake(0, 0, 80, 50);
    
    [right setTitle:@"Share" forState:UIControlStateNormal];
    [right addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton=[[UIBarButtonItem alloc]initWithCustomView:right];
    self.navigationItem.rightBarButtonItem=rightButton;

    //页眉
    
    self.backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    
    [self loadData];
    self.tableView.backgroundView =self.backImageView;
}

-(void)shareAction{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"52b13dc556240b55870e7148"
                                      shareText:@"友盟社会化分享让您快速实现分享等社会化功能，http://umeng.com/social"
                                     shareImage:nil
                                shareToSnsNames:@[UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]
                                       delegate:self];
}

- (void)loadData{
    // fix me:
    NSString *urlString = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@",self.spot_id];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self parserData:responseObject];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
}
- (void)parserData:(NSDictionary *)dict{
    
    NSDictionary *data = [dict objectForKey:@"data"];
    RmdStoryDetailBannerItem *item = [[RmdStoryDetailBannerItem alloc]init];
    NSDictionary *spot = [data objectForKey:@"spot"];
    item.date_tour = [spot objectForKey:@"date_tour"];
    item.text = [spot objectForKey:@"text"];
    item.cover_image = [spot objectForKey:@"cover_image"];
    id poi = [spot objectForKey:@"poi"];
    if (poi && [poi isKindOfClass:[NSDictionary class]]) {
        [item setupPoi:[spot objectForKey:@"poi"]];
    }
    NSDictionary *trip = [data objectForKey:@"trip"];
    item.name = [trip objectForKey:@"name"];
    [item setupUser:[trip objectForKey:@"user"]];
    [self updateTableHeader:item];
    
    NSArray *detail_list = [spot objectForKey:@"detail_list"];
    for (NSDictionary *dic in detail_list) {
        RmdStoryDetailListItem *items = [[RmdStoryDetailListItem alloc]init];
        items.text = [dic objectForKey:@"text"];
        items.photo = [dic objectForKey:@"photo"];
        [self.listArray addObject:items];
    }
    //设置背景图片
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:self.rmdStoryDetailBannerItem.cover_image] placeholderImage:nil];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.tableView reloadData];
        
    });
    
    
}

- (void)updateTableHeader:(RmdStoryDetailBannerItem *)item {
    if (nil == self.rmdStoryDetailHeaderView) {
        self.rmdStoryDetailHeaderView = [[RmdStoryDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)];
    }
    [self.rmdStoryDetailHeaderView setData:item];
    self.rmdStoryDetailHeaderView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = nil;
    self.tableView.tableHeaderView = self.rmdStoryDetailHeaderView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellIndentifier = @"RmdStoryDetailCell";
    RmdStoryDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[ RmdStoryDetailCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellIndentifier];
    }
    RmdStoryDetailListItem *item = self.listArray[indexPath.row];
    [cell setData:item];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RmdStoryDetailCell *cell = [[ RmdStoryDetailCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    RmdStoryDetailListItem *item = self.listArray[indexPath.row];
    return [cell setData:item];
}










@end
